package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class workreservationchange
 */
@WebServlet("/workreservationchange")
public class workreservationchange extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public workreservationchange() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String stuid = request.getParameter("stuid");
		String reid = request.getParameter("reid");
		String date = request.getParameter("date");
		String remarks = request.getParameter("remarks");
		String attend = request.getParameter("attend");

		String change = "UPDATE reservation set  date='" + date + "', remarks='" + remarks + "', attend='" + attend
				+ "' where reid='" + reid + "'";
		Jdbc j = null;
		try {
			j = new Jdbc(change);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (j.count > 0) {

			request.getRequestDispatcher("person_reservation.jsp?stuid=" + stuid + "").forward(request, response);
		}
		// 添加一个验证
		String check1 = "select*from reservation where stuid='" + stuid + "' ";
		System.out.println("-=========");
		System.out.println(stuid);
		Jdbc j2 = null;
		Jdbc j3 = null;
		try {
			j2 = new Jdbc();
			j3 = new Jdbc();
			j2.Jdbc_Q2(check1);
			j2.rs.last();// 将光标移到最后一行
			j2.rs.getRow();
			// 还有预约记录，那么就将sales 更新为已预约，并且查看最新的一条预约记录是否到访
			String check4 = "update sales set reservation='已预约' where id = '" + stuid + "'";
			j3 = new Jdbc(check4);// 将数据库更新
			j2.rs.first();
            String attend1 = j2.rs.getString("attend");
			
			if (attend1.equals("") || attend1.equals("未出席")||attend1.equals("未记录")) {
				String check5 = "update sales set attend='未出席' where id = '" + stuid + "'";
				j3 = new Jdbc(check5);
			}
			else {
				String check6 = "update sales set attend='已出席' where id = '"+stuid+"'";
				j3 = new Jdbc(check6);
			}

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
